package com.example.ixform.controllers;



import com.example.ixform.services.NetizenService;
import com.example.ixform.entities.Netizen;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("netizens")
public class NetizenController {

    @Autowired
    NetizenService netizenService;

    @GetMapping
    public String displayNetizens(Model model){

        List<Netizen> netizens= netizenService.getAll();
        model.addAttribute("netizens",netizens);

        return "netizens/list-netizens";
    }

    @GetMapping("/new")
    public String displayNetizenForm(Model model){

        Netizen aNetizen = new Netizen();
        model.addAttribute("netizen",aNetizen);
        return "netizens/new-netizen";
    }

    @PostMapping("/save")
    public String createNetizen(Model model,Netizen netizen){

        netizenService.save(netizen);

        return "redirect:/netizens";
    }
}
